//
//  HomePageVC.m
//  ZhiHuDaily
//
//  Created by 吴宜昊 on 2022/2/4.
//

#import "HomePageVC.h"
#import "HomePageVM.h"
#import "HomePageCell.h"
#import "HomePageHeader.h"
#import "HomePageNavBar.h"
#import "HomePageSectionHeader.h"

@interface HomePageVC () <UITableViewDelegate , UITableViewDataSource>
@property(nonatomic,strong) UITableView * tableView;
@property(nonatomic,strong) HomePageVM * viewModel;
@property(nonatomic,strong) HomePageHeader * tbHeader;
@property(nonatomic,strong) HomePageNavBar * navBar;
@end

@implementation HomePageVC {
    NSString * HomePageCellId;
    NSString * HomePageSectionHeaderId;
    CGFloat tbHeaderHeight;
    CGFloat tbSectionHeaderHeight;
}

#pragma mark - override
-(instancetype) init {
    self = [super init];
    if (self) {
        HomePageCellId = @"HomePageCellId";
        HomePageSectionHeaderId = @"HomePageSectionHeaderId";
        tbHeaderHeight = kScreenWidth;
        tbSectionHeaderHeight = 60.f;
        self.allowPopGesture = NO;
        [self setupUI];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self.viewModel requestLatest];
}

-(void) viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:YES animated:NO];
}

-(void) updateViewConstraints {
    [self remakeMas];
    [super updateViewConstraints];
}

#pragma mark - setupUI
-(void) setupUI {
    [self.view addSubview:self.navBar];
    [self.view addSubview:self.tableView];
    [self addRefresh];
    [self.view setNeedsUpdateConstraints];
}

-(void) addRefresh {
    WeakSelf(self);
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        StrongSelf(weakSelf);
        [strongSelf.viewModel requestLatest];
    }];
    
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        StrongSelf(weakSelf);
        [strongSelf.viewModel requestBefore];
    }];
}

#pragma mark - remakeMas
-(void) remakeMas {
    WeakSelf(self);
    [self.navBar mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(@0);
        make.height.equalTo(@(kTopHeight));
    }];
    
    [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        StrongSelf(weakSelf);
        make.left.right.bottom.equalTo(@0);
        make.top.equalTo(strongSelf.navBar.mas_bottom);
    }];
}

#pragma mark - lazy
-(HomePageVM *)viewModel {
    if (!_viewModel) {
        _viewModel = [HomePageVM new];
        _viewModel.bindVC = self;
    }
    return _viewModel;
}

-(UITableView *) tableView {
    if (!_tableView) {
        CGRect frame = CGRectMake(0, tbHeaderHeight, kScreenWidth, kScreenHeight - tbHeaderHeight );
        _tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.tableHeaderView = self.tbHeader;
        _tableView.tableFooterView = [UIView new];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:HomePageCell.class forCellReuseIdentifier:HomePageCellId];
        [_tableView registerClass:HomePageSectionHeader.class forHeaderFooterViewReuseIdentifier:HomePageSectionHeaderId];
        
        if (@available(iOS 15.0, *)) {
            self.tableView.sectionHeaderTopPadding = 0;
        } else {
            // Fallback on earlier versions
        }
    }
    return _tableView;
}

-(HomePageHeader *) tbHeader {
    if (!_tbHeader) {
        CGRect frame = CGRectMake(0, 0, kScreenWidth, tbHeaderHeight);
        _tbHeader = [[HomePageHeader alloc] initWithFrame:frame delegate:self];
    }
    return _tbHeader;
}

-(HomePageNavBar *) navBar {
    if (!_navBar) {
        CGRect frame = CGRectMake(0, 0, kScreenWidth, kTopHeight);
        _navBar = [[HomePageNavBar alloc] initWithFrame:frame];
    }
    return _navBar;
}

#pragma mark - UITableViewDelegate , UITableViewDataSource
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    return self.viewModel.arrayList.count;
}

-(UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return [UIView new];
    }
    
    HomePageSectionHeader * headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:HomePageSectionHeaderId];
    [headerView updateDate:self.viewModel.arrayDate[section]];
    return headerView;
}

-(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0.1;
    }
    return tbSectionHeaderHeight;
}

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HomePageCell * cell = [tableView dequeueReusableCellWithIdentifier:HomePageCellId];
    cell.model = self.viewModel.arrayList[indexPath.section][indexPath.row];
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger) section {
    return self.viewModel.arrayList[section].count;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HomePageM * model = self.viewModel.arrayList[indexPath.section][indexPath.row];
    NSLog(@"DX:%@",model.url);
    
    WebPageVC * vc = [[WebPageVC alloc] initWithURL: model.url];
    [self.navigationController pushViewController:vc animated:YES];
}

-(void) scrollViewDidScroll:(UIScrollView *) scrollView {
     if ( scrollView.contentOffset.y <= tbSectionHeaderHeight
         && scrollView.contentOffset.y >= 0 ) {
         scrollView.contentInset = UIEdgeInsetsZero;
     } else if (scrollView.contentOffset.y >= tbSectionHeaderHeight) {
         scrollView.contentInset = UIEdgeInsetsMake(-tbSectionHeaderHeight, 0, 0, 0);
    }
}

#pragma mark - SDCycleScrollViewDelegate
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger) index {
    HomeBannerM * model = self.viewModel.arrayBanner[index];
    NSLog(@"DX:%@",model.url);
    
    WebPageVC * vc = [[WebPageVC alloc] initWithURL:model.url];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - OPEN API
-(void) reloadTableView {
    [self.tableView reloadData];
}

-(void) reloadBanner:(NSArray *) urls titles:(NSArray *) titles {
    [self.tbHeader reloadBanner:urls titles:titles];
}

-(void) updateDate:(DateModel *) model {
    [self.navBar updateDate:model];
}

-(void) endRefresh {
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
}
@end
